﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.ajax
{
    public partial class product : System.Web.UI.Page
    {
        Maticsoft.BLL.InventorySize dbllsize = new Maticsoft.BLL.InventorySize();
        Maticsoft.BLL.Inventory dbll = new Maticsoft.BLL.Inventory();
        Maticsoft.BLL.InventoryClass dbll2 = new Maticsoft.BLL.InventoryClass();
        Maticsoft.BLL.size sizebll = new Maticsoft.BLL.size();
        Maticsoft.BLL.Dictionary bll = new Maticsoft.BLL.Dictionary();
        Maticsoft.BLL.productsconnection pcbll = new Maticsoft.BLL.productsconnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "upprice")
                {
                    Maticsoft.Model.Inventory mod = dbll.GetModel(Convert.ToInt32(Request["id"]));
                    mod.iInvSCost = Convert.ToDecimal(Request["price"]);
                    Response.Write((dbll.Update(mod) ? "1" : "0"));
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "connection")
                {
                    List<Maticsoft.Model.productsconnection> lists = pcbll.GetModelList("cInvCCode='" + Request["id"] + "'");//获取已经设置了的
                    List<Maticsoft.Model.InventoryClass> ilists = dbll2.GetModelList("cInvCCode in (" + Request["ids"] + ")");//获取正要设置的
                    if (lists != null)
                        foreach (var item in lists)
                        {
                            //如果已经存在则修改
                            if (ilists.Any(c => item.cInvCCode == Request["id"] && c.cInvCCode == item.connectioncInvCCode))
                            {
                                item.connectioncInvCName = ilists.First(c => c.cInvCCode == item.connectioncInvCCode).cInvCName;
                                pcbll.Update(item);
                            }
                            //如果不存在则删除
                            else
                            {
                                pcbll.Delete(item.id);
                            }
                        }
                    if (ilists != null)
                        foreach (var item in ilists)
                        {
                            if (!lists.Any(c => c.connectioncInvCCode == item.cInvCCode && c.cInvCCode == Request["id"]))
                            {
                                Maticsoft.Model.productsconnection md = new Maticsoft.Model.productsconnection();
                                md.cInvCCode = Request["id"];
                                md.connectioncInvCCode = item.cInvCCode;
                                md.connectioncInvCName = item.cInvCName;
                                pcbll.Add(md);
                            }
                        }
                    Response.Write("1");
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "setspecialvalue")
                {
                    Maticsoft.Model.InventorySize md = new Maticsoft.Model.InventorySize();
                    if (string.IsNullOrEmpty(Request["id"]))
                    {
                        md.cInvCode = Request["cInvCode"];
                        md.value = Request["value"];
                        md.name = Request["name"];
                        Response.Write(dbllsize.Add(md) > 0 ? "1" : "");
                    }
                    else
                    {
                        md = dbllsize.GetModel(Convert.ToInt32(Request["id"]));
                        md.value = Request["value"];
                        md.name = Request["name"];
                        Response.Write(dbllsize.Update(md) ? "2" : "");
                    }
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "getsize")
                {
                    List<Maticsoft.Model.size> list = sizebll.GetModelList("cInvStd='" + Request["cInvStd"] + "' order by id desc");
                    if (string.IsNullOrEmpty(Request["str"]))
                    {
                        Response.Write(JsonConvert.SerializeObject(new { group = list.Select(c => new { c.code }).Distinct(), list = list }));
                    }
                    else
                    {
                        //List<Maticsoft.Model.size> list = sizebll.GetModelList("cInvStd='" + Request["cInvStd"] + "' and code='" + Request["str"] + "'");
                        Response.Write(JsonConvert.SerializeObject(new { group = list.Select(c => new { c.code }).Distinct(), list = list.Where(c => c.code == Request["str"]) }));
                    }
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "getsizelist")
                {
                    List<Maticsoft.Model.size> list = sizebll.GetModelList("cInvStd='" + Request["cInvStd"] + "'");
                    if (!string.IsNullOrEmpty(Request["size"]))
                        Response.Write(JsonConvert.SerializeObject(new { sizelist = list.Select(c => new { c.code }).Distinct(), list = list.Where(c => c.code == Request["size"]) }));
                    else
                        Response.Write(JsonConvert.SerializeObject(new { sizelist = list.Select(c => new { c.code }).Distinct(), list = list.Where(c => c.code == list.Select(a => new { a.code }).Distinct().First().code) }));
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "gettetisize")
                {
                    List<Maticsoft.Model.InventorySize> list = dbllsize.GetModelList("cInvCode='" + Request["cInvCode"] + "'");
                    Response.Write(JsonConvert.SerializeObject(list));
                }
                else if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "gettetisizegroupby")
                {
                    if (!string.IsNullOrEmpty(Request["cInvStd"]))
                    {
                        List<Maticsoft.Model.InventorySize> list = dbllsize.GetModelList("cInvCode ='" + Request["cInvStd"] + "'");
                        Response.Write(JsonConvert.SerializeObject(list.Select(c => new { groupname = c.name, codelist = c.value.Split(',') })));
                    }
                    else
                        Response.Write("[]");
                }
                else
                {
                    Response.Write(string.Format("{0}", (dbll.GetproductsList(Request["id"], "bSale", "05") && dbll2.GetInventoryClassList(Request["id"], "05")) ? "1" : "0"));
                }
            }
        }
    }
}